package br.com.petsis.conversores;

import java.text.ParseException;
import java.util.Date;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import br.com.petsis.utilitarios.UtilDate;
import br.com.petsis.utilitarios.UtilString;

public class ConversorData implements Converter {

	public Object getAsObject(FacesContext context, UIComponent component, String valorCampo){
		Object data = null;
		if(valorCampo != null) {
			if(!UtilString.isStringVazia(valorCampo)){
				try {
					data = UtilDate.getData(valorCampo);
				} catch (ParseException e) {
					throw new RuntimeException(e);
				}
			}
		}
		return data;
	}

	public String getAsString(FacesContext context, UIComponent component, Object objCampo){
		String data = null;
		if(objCampo != null) {
			if(!UtilString.isStringVazia(objCampo.toString())){
				data = UtilDate.getDataComoString((Date)objCampo,UtilDate.FORMATO_DATA_PADRAO);
			}
		}
		return data;
	}
	
}
